package com.blr.service;

import com.blr.dao.UserMapper;
import com.blr.entity.Role;
import com.blr.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: UserService
 * Description:
 * Date: 2022/11/16 PM 6:15:01
 * <p>
 * project: codes
 * package: com.blr.service
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class UserServiceImpl implements UserDetailsService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.根据用户名查询用户信息
        User user = userMapper.loadUserByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        List<Role> roles = userMapper.getUserRoleByUid(user.getId());
        user.setRoles(roles);
        return user;
    }
}
